/**
 * @param {number[]} nums
 * @param {number} k
 * @return {void} Do not return anything, modify nums in-place instead.
*/
var rotate = function(nums, k) {
  if(nums.length == 1)
    return nums;
  if(k >= nums.length)
    k %= nums.length;
  // 逆置函数
  function reverse(arr, left, right) {
    const mid = Math.floor((right + left) / 2);
    for(let i = left; i <= mid; i++) {
      let temp = arr[i];
      arr[i] = arr[right - (i - left)];
      arr[right - (i - left)] = temp;
    }
  }
  const kk = nums.length - k;
  reverse(nums, 0, kk - 1);
  reverse(nums, kk, nums.length - 1);
  reverse(nums, 0, nums.length - 1);
  return nums;
};

let nums = [1,2,3,4,5,6,7], k = 3
const result1 = rotate(nums, k);
console.log(result1);
nums = [-1,-100,3,99], k = 2
const result2 = rotate(nums, k);
console.log(result2);
const result3 = rotate([-1], 2);
console.log(result3);
const result4 = rotate([1, 2], 3);
console.log(result4);
